<!DOCTYPE html>
<html>
<head hexo-theme='https://volantis.js.org/#2.6.5'>
  <meta charset="utf-8">
  <!-- SEO相关 -->
  
    
  
  <!-- 渲染优化 -->
  <meta name="renderer" content="webkit">
  <meta name="force-rendering" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <meta name="HandheldFriendly" content="True" >
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  <!-- 页面元数据 -->
  
    <title>利用PHPMD让你的php代码更干净更易维护 - 麦田麦穗</title>
  
    <meta name="keywords" content="PHP">
  
  
    <meta name="description" content="PHPMD - PHP Mess Detector
等价于java工具PMD，能够对php 源代码进行如下问题检测：

可能的bug
欠佳的代码
过于复杂的表达式
未使用的方法、变量、参数、属性

最新版本发布于 2021/05/11">
  

  <!-- feed -->
  

  <!-- import meta -->
  

  <!-- link -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.13/css/all.min.css">
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css">

  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-waves@0.7.6/dist/waves.min.css">

  

  

  

  

  <!-- import link -->
  

  
    
      
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.0.2/styles/monokai-sublime.css">

    
  
  
    
<link rel="stylesheet" href="/css/style.css">

  

  <script>
    function setLoadingBarProgress(num) {
      document.getElementById('loading-bar').style.width=num+"%";
    }
  </script>

  
  
    <!-- ba -->
    <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e99feed4245dcdf8a16ad3d62e236e14";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
    </script>
  
    
</head>

<body>
  
  <div id="loading-bar-wrapper">
  <div id="loading-bar"></div>
</div>
<header class="l_header shadow blur">
  <div class='container'>
  <div class='wrapper'>
    <div class='nav-sub'>
      <p class="title"></p>
      <ul class='switcher nav-list-h'>
        <li><a class="s-comment fas fa-comments fa-fw" target="_self" href='javascript:void(0)'></a></li>
        
          <li><a class="s-toc fas fa-list fa-fw" target="_self" href='javascript:void(0)'></a></li>
        
      </ul>
    </div>
		<div class="nav-main">
      
        
        <a class="title flat-box" target="_self" href='/'>
          
          
          
            麦穗
          
          
        </a>
      

			<div class='menu navigation'>
				<ul class='nav-list-h'>
          
          
          
            
            
              <li>
                <a class="flat-box" href=/
                  
                  
                  
                    id="home"
                  >
                  <i class='fas fa-rss fa-fw'></i>博客
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/categories/
                  
                  
                  
                    id="categories"
                  >
                  <i class='fas fa-folder-open fa-fw'></i>分类
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/tags/
                  
                  
                  
                    id="tags"
                  >
                  <i class='fas fa-tags fa-fw'></i>标签
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/archives/
                  
                  
                  
                    id="archives"
                  >
                  <i class='fas fa-archive fa-fw'></i>归档
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/friends/
                  
                  
                  
                    id="friends"
                  >
                  <i class='fas fa-link fa-fw'></i>友链
                </a>
                
              </li>
            
          
          
            
            
              <li>
                <a class="flat-box" href=/about/
                  
                  
                  
                    id="about"
                  >
                  <i class='fas fa-info-circle fa-fw'></i>关于
                </a>
                
              </li>
            
          
          
				</ul>
			</div>

      <div class="m_search">
        <form name="searchform" class="form u-search-form">
          <i class="icon fas fa-search fa-fw"></i>
          <input type="text" class="input u-search-input" placeholder="Search..." />
        </form>
      </div>

			<ul class='switcher nav-list-h'>
				
					<li><a class="s-search fas fa-search fa-fw" target="_self" href='javascript:void(0)'></a></li>
				
				<li>
          <a class="s-menu fas fa-bars fa-fw" target="_self" href='javascript:void(0)'></a>
          <ul class="menu-phone list-v navigation white-box">
            
              
            
              <li>
                <a class="flat-box" href=/
                  
                  
                  
                    id="home"
                  >
                  <i class='fas fa-rss fa-fw'></i>博客
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/categories/
                  
                  
                  
                    id="categories"
                  >
                  <i class='fas fa-folder-open fa-fw'></i>分类
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/tags/
                  
                  
                  
                    id="tags"
                  >
                  <i class='fas fa-tags fa-fw'></i>标签
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/archives/
                  
                  
                  
                    id="archives"
                  >
                  <i class='fas fa-archive fa-fw'></i>归档
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/friends/
                  
                  
                  
                    id="friends"
                  >
                  <i class='fas fa-link fa-fw'></i>友链
                </a>
                
              </li>
            
          
            
              
            
              <li>
                <a class="flat-box" href=/about/
                  
                  
                  
                    id="about"
                  >
                  <i class='fas fa-info-circle fa-fw'></i>关于
                </a>
                
              </li>
            
          
            
          </ul>
        </li>
			</ul>
		</div>
	</div>
  </div>
</header>

<script>setLoadingBarProgress(40);</script>



  <div class="l_body nocover">
    <div class='body-wrapper'>
      

<div class='l_main'>
  

  
    <article id="post" class="post white-box reveal shadow article-type-post" itemscope itemprop="blogPost">
      


  <section class='meta'>
    
      
      
      <div class="meta" id="header-meta">
        
          
  <h1 class="title">
    <a href="/2021/07/27/%E5%88%A9%E7%94%A8PHPMD%E8%AE%A9%E4%BD%A0%E7%9A%84php%E4%BB%A3%E7%A0%81%E6%9B%B4%E5%B9%B2%E5%87%80%E6%9B%B4%E6%98%93%E7%BB%B4%E6%8A%A4/">
      利用PHPMD让你的php代码更干净更易维护
    </a>
  </h1>


        
        <div class='new-meta-box'>
          
            
          
            
              
<div class='new-meta-item author'>
  <a href="" rel="nofollow">
    <img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210603145600.png">
    <p>vison</p>
  </a>
</div>

            
          
            
              

            
          
            
              <div class="new-meta-item date">
  <a class='notlink'>
    <i class="fas fa-calendar-alt fa-fw" aria-hidden="true"></i>
    <p>发布于：Jul 27, 2021</p>
  </a>
</div>

            
          
            
              

            
          
        </div>
        
          <hr>
        
      </div>
    
  </section>


      <section class="article typo">
        <div class="article-entry" itemprop="articleBody">
          
          <p>PHPMD - PHP Mess Detector</p>
<p>等价于java工具PMD，能够对php 源代码进行如下问题检测：</p>
<ul>
<li>可能的bug</li>
<li>欠佳的代码</li>
<li>过于复杂的表达式</li>
<li>未使用的方法、变量、参数、属性</li>
</ul>
<p>最新版本发布于 2021/05/11</p>
<span id="more"></span>

<p><a target="_blank" rel="noopener" href="https://phpmd.org/about.html">https://phpmd.org/about.html</a></p>
<h2 id="基本使用"><a href="#基本使用" class="headerlink" title="基本使用"></a>基本使用</h2><pre><code class="shell">phpmd /path/to/source text codesize</code></pre>
<ul>
<li>第一个参数 要检测的代码地址</li>
<li>第二个参数指定输出检测结果的格式</li>
<li>第三个参数是指定的规则集</li>
</ul>
<h2 id="规则集合"><a href="#规则集合" class="headerlink" title="规则集合"></a>规则集合</h2><ul>
<li><code>Clean Code</code>: 一些包含clean code的规则集合，包括面向对象设计的 SOLID（单一功能、开闭原则、里氏替换、接口隔离以及依赖反转）原则。</li>
<li><code>Code Size</code> : 有关代码大小的相关问题，例如行数等</li>
<li><code>Controversial</code> : 具有争议的规则，可以不参考此规则</li>
<li><code>Design</code> ： 设计规则，包含类依赖数量等等</li>
<li><code>Naming</code>: 命名规则</li>
<li><code>Unused Code</code>: 未使用代码规则</li>
</ul>
<p>关于<code>clean code</code> 有一本书名就叫 &lt;&lt; Clean Code &gt;&gt;,个人读下来感受颇深。 </p>
<p>这里主要介绍一些我认为非常有必要注意的规则</p>
<h2 id="Clean-Code"><a href="#Clean-Code" class="headerlink" title="Clean Code"></a>Clean Code</h2><h3 id="BooleanArgumentFlag"><a href="#BooleanArgumentFlag" class="headerlink" title="BooleanArgumentFlag"></a>BooleanArgumentFlag</h3><p>布尔标志参数违反单一责任原则（SRP）,可以将此类方法一拆为二。</p>
<pre><code class="php">class Foo &#123;
    public function drink($thirsty = true) &#123;
        if($thirsty)&#123;
            // 喝2杯
        &#125;else&#123;
            // 喝一杯
        &#125;
    &#125;
&#125;</code></pre>
<pre><code class="php">class Foo &#123;
    public function drinkThirsty() &#123;

            // 喝2杯

    &#125;

      public function drinkUnThirsty() &#123;

            // 喝2杯

    &#125;
&#125;</code></pre>
<h3 id="ElseExpression"><a href="#ElseExpression" class="headerlink" title="ElseExpression"></a>ElseExpression</h3><pre><code class="php">class Foo
&#123;
    public function bar($flag)
    &#123;
        if ($flag) &#123;
            // one branch
        &#125; else &#123;
            // another branch
        &#125;
    &#125;
&#125;</code></pre>
<p>像这样的if else的表达式其实是没必要的，可通过三元表达式、拆分方法或者可能的情况下先return来规避这类问题，以将代码简单或增加可读性。</p>
<pre><code class="php">class Foo
&#123;
    public function bar($flag)
    &#123;
        if ($flag) &#123;
            // one branch
            return
        &#125; 
        // another branch

    &#125;
&#125;</code></pre>
<h2 id="Code-Size"><a href="#Code-Size" class="headerlink" title="Code Size"></a>Code Size</h2><h3 id="CyclomaticComplexity"><a href="#CyclomaticComplexity" class="headerlink" title="CyclomaticComplexity"></a>CyclomaticComplexity</h3><p>圈复杂度，用来衡量代码复杂度的一个计算规则。复杂度越高代表代码可读性、可维护性越差，易错性更高、集成测试更难。</p>
<p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210727111045.png" alt=""></p>
<h4 id="点边计算法"><a href="#点边计算法" class="headerlink" title="点边计算法"></a>点边计算法</h4><p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210727110713.png" alt=""></p>
<p>上述表达式的圈复杂度为 e = 10 n = 8 Cyclomatic Complexity = 10 - 8 + 2 = 4</p>
<p><img src="https://vison-blog.oss-cn-beijing.aliyuncs.com/20210727111603.png" alt=""></p>
<p>计算公式为：</p>
<p>V(G) = E - N + 2</p>
<p>其中，e表示控制流图中边的数量，n表示控制流图中节点的数量。</p>
<p>更多可参考 <a target="_blank" rel="noopener" href="http://kaelzhang81.github.io/2017/06/18/%E8%AF%A6%E8%A7%A3%E5%9C%88%E5%A4%8D%E6%9D%82%E5%BA%A6/">http://kaelzhang81.github.io/2017/06/18/详解圈复杂度/</a></p>
<h3 id="ExcessiveMethodLength"><a href="#ExcessiveMethodLength" class="headerlink" title="ExcessiveMethodLength"></a>ExcessiveMethodLength</h3><p>方法长度，该规则有2个属性</p>
<p>minimum 最小限定值 默认 100</p>
<p>ignore-whitespace 是否忽略空白行 默认 false</p>
<h3 id="ExcessiveClassLength"><a href="#ExcessiveClassLength" class="headerlink" title="ExcessiveClassLength"></a>ExcessiveClassLength</h3><p>类长度</p>
<p>minimum 最小限定值 默认 1000</p>
<p>ignore-whitespace 是否忽略空白行 默认 false</p>
<h3 id="ExcessiveParameterList"><a href="#ExcessiveParameterList" class="headerlink" title="ExcessiveParameterList"></a>ExcessiveParameterList</h3><p>参数个数限定</p>
<p>minimum 10，根据《clean code》一书的说法，这个值应该限定为3</p>
<h3 id="ExcessivePublicCount"><a href="#ExcessivePublicCount" class="headerlink" title="ExcessivePublicCount"></a>ExcessivePublicCount</h3><p>公共方法、公共属性</p>
<p>minimum    45</p>
<h3 id="TooManyFields"><a href="#TooManyFields" class="headerlink" title="TooManyFields"></a>TooManyFields</h3><p>maxfields    15</p>
<h3 id="TooManyMethods"><a href="#TooManyMethods" class="headerlink" title="TooManyMethods"></a>TooManyMethods</h3><p>maxmethods    25<br>ignorepattern    (^(set|get))i</p>
<h3 id="TooManyPublicMethods"><a href="#TooManyPublicMethods" class="headerlink" title="TooManyPublicMethods"></a>TooManyPublicMethods</h3><p>maxmethods    10    The method count reporting threshold<br>ignorepattern    (^(set|get))i</p>
<h2 id="自定义规则"><a href="#自定义规则" class="headerlink" title="自定义规则"></a>自定义规则</h2><p>有的默认规则也许并不能满足自身需求需做舍弃或修改。例如函数参数数，在《clean code》一书中定义的是最多为3，而phpmd默认指定的是10. 10个确实已经非常难看了，想象你去调用一个有10个参数的函数，你一定会吐槽懵逼的。</p>
<p>好在phpmd这些都可以修改，如下：</p>
<ul>
<li>引入unusedcode规则集</li>
<li>引入codesize,并暂时排除<code>NPathComplexity</code>和<code>CyclomaticComplexity</code>这两个理解起来有一定困难的规则。诚然<code>CyclomaticComplexity</code>在度量复杂度十分有效，但若你的所有函数已经满足<code>ExcessiveMethodLength</code>已经进步很大了，所以一步步来。</li>
<li>修改ExcessiveParameterList,让限定值为3.</li>
<li>修改ExcessiveMethodLength和ExcessiveClassLength,忽略空白行</li>
<li>排除StaticAccess规则</li>
</ul>
<pre><code class="xml">&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;ruleset name=&quot;My first PHPMD rule set&quot;
         xmlns=&quot;http://pmd.sf.net/ruleset/1.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://pmd.sf.net/ruleset/1.0.0
                     http://pmd.sf.net/ruleset_xml_schema.xsd&quot;
         xsi:noNamespaceSchemaLocation=&quot;
                     http://pmd.sf.net/ruleset_xml_schema.xsd&quot;&gt;
    &lt;description&gt;
        My custom rule set that checks my code...
    &lt;/description&gt;

    &lt;rule ref=&quot;rulesets/unusedcode.xml&quot; /&gt;
    &lt;rule ref=&quot;rulesets/codesize.xml&quot;&gt;
        &lt;exclude name=&quot;ExcessiveParameterList&quot; /&gt;
        &lt;exclude name=&quot;NPathComplexity&quot;/&gt;
        &lt;exclude name=&quot;CyclomaticComplexity&quot;/&gt;
    &lt;/rule&gt;
    &lt;rule ref=&quot;rulesets/codesize.xml/ExcessiveParameterList&quot;&gt;
        &lt;properties&gt;
            &lt;property name=&quot;minimum&quot;&gt;
                &lt;value&gt;
                    3
                &lt;/value&gt;
            &lt;/property&gt;
        &lt;/properties&gt;
    &lt;/rule&gt;
    &lt;rule ref=&quot;rulesets/codesize.xml/ExcessiveClassLength&quot;&gt;
        &lt;properties&gt;
            &lt;property name=&quot;ignore-whitespace&quot;&gt;
                &lt;value&gt;
                    true
                &lt;/value&gt;
            &lt;/property&gt;
        &lt;/properties&gt;
    &lt;/rule&gt;
    &lt;rule ref=&quot;rulesets/codesize.xml/ExcessiveMethodLength&quot;&gt;
        &lt;properties&gt;
            &lt;property name=&quot;ignore-whitespace&quot;&gt;
                &lt;value&gt;
                    true
                &lt;/value&gt;
            &lt;/property&gt;
        &lt;/properties&gt;
    &lt;/rule&gt;
    &lt;rule ref=&quot;rulesets/cleancode.xml&quot;&gt;
        &lt;exclude name=&quot;StaticAccess&quot; /&gt;
    &lt;/rule&gt;
&lt;/ruleset&gt;</code></pre>

          
            <div class='article_footer'>
              
                
  
    
    



  

  
    
    



  

  
    
    

<section class="widget copyright  desktop mobile">
  <div class='content'>
    
      <blockquote>
        
          
            <p>博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议</p>

          
        
          
            <p>本文永久链接是：<a href=http://blog.visonforcoding.xyz/2021/07/27/%E5%88%A9%E7%94%A8PHPMD%E8%AE%A9%E4%BD%A0%E7%9A%84php%E4%BB%A3%E7%A0%81%E6%9B%B4%E5%B9%B2%E5%87%80%E6%9B%B4%E6%98%93%E7%BB%B4%E6%8A%A4/>http://blog.visonforcoding.xyz/2021/07/27/%E5%88%A9%E7%94%A8PHPMD%E8%AE%A9%E4%BD%A0%E7%9A%84php%E4%BB%A3%E7%A0%81%E6%9B%B4%E5%B9%B2%E5%87%80%E6%9B%B4%E6%98%93%E7%BB%B4%E6%8A%A4/</a></p>
          
        
      </blockquote>
    
  </div>
</section>

  

  
    
    

<section class="widget qrcode  desktop mobile">
  

  <div class='content article-entry'>
    
      
        <div class='fancybox'><img src='https://vison-blog.oss-cn-beijing.aliyuncs.com/20210430145030.png'
        
          height='240px'
        ></div>
      
    
    请我喝杯咖啡
  </div>
</section>

  


              
            </div>
          
        </div>
        
          


  <section class='meta' id="footer-meta">
    <div class='new-meta-box'>
      
        
          <div class="new-meta-item date" itemprop="dateUpdated" datetime="2021-07-27T14:03:48+08:00">
  <a class='notlink'>
    <i class="fas fa-edit fa-fw" aria-hidden="true"></i>
    <p>更新于：Jul 27, 2021</p>
  </a>
</div>

        
      
        
          
  
  <div class="new-meta-item meta-tags"><a class="tag" href="/tags/PHP/" rel="nofollow"><i class="fas fa-hashtag fa-fw" aria-hidden="true"></i><p>PHP</p></a></div>


        
      
        
          

        
      
        
          
  <div class="new-meta-item share -mob-share-list">
  <div class="-mob-share-list share-body">
    
      
        <a class="-mob-share-qq" title="" rel="external nofollow noopener noreferrer noopener"
          
          target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://blog.visonforcoding.xyz/2021/07/27/%E5%88%A9%E7%94%A8PHPMD%E8%AE%A9%E4%BD%A0%E7%9A%84php%E4%BB%A3%E7%A0%81%E6%9B%B4%E5%B9%B2%E5%87%80%E6%9B%B4%E6%98%93%E7%BB%B4%E6%8A%A4/&title=利用PHPMD让你的php代码更干净更易维护 - 麦田麦穗&summary=PHPMD - PHP Mess Detector
等价于java工具PMD，能够对php 源代码进行如下问题检测：

可能的bug
欠佳的代码
过于复杂的表达式
未使用的方法、变量、参数、属性

最新版本发布于 2021/05/11"
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/qq.png">
          
        </a>
      
    
      
        <a class="-mob-share-qzone" title="" rel="external nofollow noopener noreferrer noopener"
          
          target="_blank" href="https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=http://blog.visonforcoding.xyz/2021/07/27/%E5%88%A9%E7%94%A8PHPMD%E8%AE%A9%E4%BD%A0%E7%9A%84php%E4%BB%A3%E7%A0%81%E6%9B%B4%E5%B9%B2%E5%87%80%E6%9B%B4%E6%98%93%E7%BB%B4%E6%8A%A4/&title=利用PHPMD让你的php代码更干净更易维护 - 麦田麦穗&summary=PHPMD - PHP Mess Detector
等价于java工具PMD，能够对php 源代码进行如下问题检测：

可能的bug
欠佳的代码
过于复杂的表达式
未使用的方法、变量、参数、属性

最新版本发布于 2021/05/11"
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/qzone.png">
          
        </a>
      
    
      
        <a class="-mob-share-weibo" title="" rel="external nofollow noopener noreferrer noopener"
          
          target="_blank" href="http://service.weibo.com/share/share.php?url=http://blog.visonforcoding.xyz/2021/07/27/%E5%88%A9%E7%94%A8PHPMD%E8%AE%A9%E4%BD%A0%E7%9A%84php%E4%BB%A3%E7%A0%81%E6%9B%B4%E5%B9%B2%E5%87%80%E6%9B%B4%E6%98%93%E7%BB%B4%E6%8A%A4/&title=利用PHPMD让你的php代码更干净更易维护 - 麦田麦穗&summary=PHPMD - PHP Mess Detector
等价于java工具PMD，能够对php 源代码进行如下问题检测：

可能的bug
欠佳的代码
过于复杂的表达式
未使用的方法、变量、参数、属性

最新版本发布于 2021/05/11"
          
          >
          
            <img src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-assets/logo/128/weibo.png">
          
        </a>
      
    
  </div>
</div>



        
      
    </div>
  </section>


        
        
          <div class="prev-next">
            
              <a class='prev' href='/2021/07/30/%E4%BB%A3%E7%A0%81%E6%95%B4%E6%B4%81%E4%B9%8B%E9%81%93/'>
                <p class='title'><i class="fas fa-chevron-left" aria-hidden="true"></i>代码整洁之道</p>
                <p class='content'>软件质量不但依赖于架构及项目管理，还与代码质量紧密相关。

代码质量与整洁度成正比，干净的代码即在质量上较为可靠，也为后期维护、升级奠定良好基础。

另一种概念叫做极限编程
童子军军规让营地比你...</p>
              </a>
            
            
              <a class='next' href='/2021/07/23/%E7%94%A8phpunit%E8%BF%9B%E8%A1%8C%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/'>
                <p class='title'>用phpunit进行单元测试<i class="fas fa-chevron-right" aria-hidden="true"></i></p>
                <p class='content'>最近读了一本书《clean code》。深有感悟，感觉非常接底气，讲到了日常编码中真正遇到的困扰。大致我总结下就是要做几件事：

重构，定期重构，保持重构
clean code,编写整洁、易维护...</p>
              </a>
            
          </div>
        
      </section>
    </article>
  

  
    <!-- 显示推荐文章和评论 -->



  <article class="post white-box reveal comments shadow">
    <section class="article typo">
      <p ct><i class='fas fa-comments'></i> 评论</p>
      
      
      
      
        <section id="comments">
          <div id="gitalk-container"></div>
        </section>
      
      
      
      
    </section>
  </article>


  




<!-- 根据页面mathjax变量决定是否加载MathJax数学公式js -->



  <script>
    window.subData = {
      title: '利用PHPMD让你的php代码更干净更易维护',
      tools: true
    }
  </script>


</div>
<aside class='l_side'>
  
  

  
    
    


  <section class="widget toc-wrapper shadow desktop mobile">
    
  <header>
    
      <i class="fas fa-list fa-fw" aria-hidden="true"></i><span class='name'>本文目录</span>
    
  </header>


    <div class='content'>
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="toc-text">基本使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%A7%84%E5%88%99%E9%9B%86%E5%90%88"><span class="toc-text">规则集合</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Clean-Code"><span class="toc-text">Clean Code</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#BooleanArgumentFlag"><span class="toc-text">BooleanArgumentFlag</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ElseExpression"><span class="toc-text">ElseExpression</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Code-Size"><span class="toc-text">Code Size</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#CyclomaticComplexity"><span class="toc-text">CyclomaticComplexity</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%82%B9%E8%BE%B9%E8%AE%A1%E7%AE%97%E6%B3%95"><span class="toc-text">点边计算法</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ExcessiveMethodLength"><span class="toc-text">ExcessiveMethodLength</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ExcessiveClassLength"><span class="toc-text">ExcessiveClassLength</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ExcessiveParameterList"><span class="toc-text">ExcessiveParameterList</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ExcessivePublicCount"><span class="toc-text">ExcessivePublicCount</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#TooManyFields"><span class="toc-text">TooManyFields</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#TooManyMethods"><span class="toc-text">TooManyMethods</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#TooManyPublicMethods"><span class="toc-text">TooManyPublicMethods</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%A7%84%E5%88%99"><span class="toc-text">自定义规则</span></a></li></ol>
    </div>
  </section>


  


</aside>


  
  <footer class="clearfix">
    <br><br>
    
      
        <div class="aplayer-container">
          


        </div>
      
    
      
        <br>
        <div class="social-wrapper">
          
            
              <a href="/atom.xml"
                class="social fas fa-rss flat-btn"
                target="_blank"
                rel="external nofollow noopener noreferrer">
              </a>
            
          
            
              <a href="mailto:visonforcoding@gmail.com"
                class="social fas fa-envelope flat-btn"
                target="_blank"
                rel="external nofollow noopener noreferrer">
              </a>
            
          
            
              <a href="https://music.163.com/#/user/home?id=63035382"
                class="social fas fa-headphones-alt flat-btn"
                target="_blank"
                rel="external nofollow noopener noreferrer">
              </a>
            
          
        </div>
      
    
      
        <div><p>Blog content follows the <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en">Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) License</a></p>
</div>
      
    
      
        Use
        <a href="https://volantis.js.org/" target="_blank" class="codename">Volantis</a>
        as theme, total visits
          <span id="busuanzi_value_site_pv"><i class="fas fa-circle-notch fa-spin fa-fw" aria-hidden="true"></i></span>
          times
        
      
    
      
        <div class='copyright'>
        <p><a target="_blank" rel="noopener" href="https://visonforcoding.github.io">Copyright © 2017-2020 Vison</a></p>

        </div>
      
    
  </footer>

<script>setLoadingBarProgress(80);</script>


      <script>setLoadingBarProgress(60);</script>
    </div>
    <a class="s-top fas fa-arrow-up fa-fw" href='javascript:void(0)'></a>
  </div>
  
<script src="https://cdn.jsdelivr.net/npm/jquery@3.4/dist/jquery.min.js"></script>


  <script>
    
    var SEARCH_SERVICE = "hexo" || "hexo";
    var ROOT = "/" || "/";
    if (!ROOT.endsWith('/')) ROOT += '/';
  </script>


  <script async src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-volantis@2/js/instant_page.js" type="module" defer integrity="sha384-OeDn4XE77tdHo8pGtE1apMPmAipjoxUQ++eeJa6EtJCfHlvijigWiJpD7VDPWXV1"></script>


  <script src="https://cdn.jsdelivr.net/npm/scrollreveal@4.0.6/dist/scrollreveal.min.js"></script>
  <script type="text/javascript">
    $(function() {
      ScrollReveal().reveal('.l_main .reveal', {
        distance: '8px',
        duration: '800',
        interval: '100',
        scale: '1'
      });
    });
  </script>


  
<script src="https://cdn.jsdelivr.net/npm/node-waves@0.7.6/dist/waves.min.js"></script>

  <script type="text/javascript">
    $(function() {
      Waves.attach('.flat-btn', ['waves-button']);
      Waves.attach('.float-btn', ['waves-button', 'waves-float']);
      Waves.attach('.float-btn-light', ['waves-button', 'waves-float', 'waves-light']);
      Waves.attach('.flat-box', ['waves-block']);
      Waves.attach('.float-box', ['waves-block', 'waves-float']);
      Waves.attach('.waves-image');
      Waves.init();
    });
  </script>


  <script async src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-busuanzi@2.3/js/busuanzi.pure.mini.js"></script>



  
  
  
    
<script src="https://cdn.jsdelivr.net/npm/jquery-backstretch@2.1.18/jquery.backstretch.min.js"></script>

    <script type="text/javascript">
      $(function(){
        var imgs=["https://cdn.jsdelivr.net/gh/xaoxuu/cdn-wallpaper/abstract/41F215B9-261F-48B4-80B5-4E86E165259E.jpeg"];
        if ('true' == 'true') {
          function shuffle(arr){
            /*From countercurrent-time*/
            var n = arr.length;
            while(n--) {
              var index = Math.floor(Math.random() * n);
              var temp = arr[index];
              arr[index] = arr[n];
              arr[n] = temp;
            }
          }
          shuffle(imgs);
        }
        if ('.cover') {
          $('.cover').backstretch(
            imgs,
          {
            duration: "20000",
            fade: "1500"
          });
        } else {
          $.backstretch(
            imgs,
          {
            duration: "20000",
            fade: "1500"
          });
        }
      });
    </script>
  








  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
  <script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
  <script type="text/javascript">
    var gitalk = new Gitalk({
      clientID: "bd4d3e0f680affe497d0",
      clientSecret: "a31430007d9952541a4322ebf7219a0d127cc57d",
      repo: "visonforcoding.github.io",
      owner: "visonforcoding",
      admin: "visonforcoding",
      
        id: (location.pathname.split("/").slice(-2)[0]).substring(0,49),      // Ensure uniqueness and length less than 50
      
      distractionFreeMode: false  // Facebook-like distraction free mode
    });
    gitalk.render('gitalk-container');
  </script>








  
<script src="/js/app.js"></script>



  
<script src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-volantis@2.6.5/js/search.js"></script>



  
<script src="https://cdn.jsdelivr.net/gh/xaoxuu/cdn-volantis@2/js/comment_typing.js"></script>




  
    
<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.18.1/build/highlight.min.js"></script>

    <script>hljs.initHighlightingOnLoad();</script>
  



<!-- 复制 -->

  <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="fas fa-copy"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('fa-copy');
        $icon.addClass('fa-check-circle');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('fa-check-circle');
          $icon.addClass('fa-copy');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('fa-copy');
        $icon.addClass('fa-times-circle');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('fa-times-circle');
          $icon.addClass('fa-copy');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>




<!-- fancybox -->

  <script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
<script>
  function pjax_fancybox() {
    let LAZY_LOAD_IMAGE = "";
    $(".article-entry").find("img").not('.inline').not('a img').each(function () { //渲染 fancybox
      var element = document.createElement("a"); // a 标签
      $(element).attr("pjax-fancybox", "");  // 过滤 pjax
      if (LAZY_LOAD_IMAGE) {
        $(element).attr("href", $(this).attr("data-original"));
      }
      $(element).attr("data-fancybox", "images");
      var caption = "";   // 描述信息
      if ($(this).attr('alt')) {  // 标准 markdown 描述信息
        $(element).attr('data-caption', $(this).attr('alt'));
        caption = $(this).attr('alt');
      }
      var div = document.createElement("div");
      $(div).addClass("fancybox");
      $(this).wrap(div); // 最外层套 div ，其实主要作用还是 class 样式
      var span = document.createElement("span");
      $(span).addClass("image-caption");
      $(span).text(caption); // 加描述
      $(this).after(span);  // 再套一层描述
      $(this).wrap(element);  // 最后套 a 标签
    })
    $(".article-entry").find("img").fancybox({
      selector: '[data-fancybox="images"]',
      hash: false,
      loop: false,
      closeClick: true,
      helpers: {
        overlay: {closeClick: true}
      },
      buttons: [
        "zoom",
        "close"
      ]
    });
  };
  $(function () {
    pjax_fancybox();
  });
</script>






  <script>setLoadingBarProgress(100);</script>  
</body>
</html>
